package com.gxc.authcore.system.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gxc.authcore.system.domain.Role;
import com.gxc.dbcore.dao.SqlDao;
import com.gxc.dbcore.service.BeanService;
import com.gxc.dbcore.util.CompareItem;
import com.gxc.dbcore.util.CompareType;
import com.gxc.dbcore.util.ConditionUtil;
import com.gxc.dbcore.util.PageInfo;
import com.gxc.dbcore.util.PageList;
import com.gxc.webcore.util.StringUtil;

@Service
public class RoleService {

	@Autowired
	private SqlDao sqlDao;
	@Autowired
	private BeanService beanService;

	public Role retrieve(String id) {
		return beanService.get(sqlDao, Role.class, id);
	}

	@SuppressWarnings("rawtypes")
	public Role retrieveByName(String name) {
		List<CompareItem> conditionList = new ArrayList<CompareItem>();
		conditionList.add(new CompareItem("name", CompareType.EQUAL, name));
		List list = beanService.list(sqlDao, Role.class, conditionList, null);
		if (list != null && list.size() > 0) {
			return (Role) list.get(0);
		}
		return null;
	}

	public int create(Role role) {
		return beanService.create(sqlDao, role);
	}

	public int update(Role role) {
		return beanService.update(sqlDao, role);
	}

	public int delete(String id) {
		return beanService.delete(sqlDao, Role.class, id);
	}

	public int delete(String[] idArray) {
		return beanService.delete(sqlDao, Role.class, idArray);
	}

	@SuppressWarnings("rawtypes")
	public List<CompareItem> buildConditions(Map params) {
		List<CompareItem> conditionList = new ArrayList<CompareItem>();
		conditionList.add(new CompareItem("id", CompareType.EQUAL));
		conditionList.add(new CompareItem("name", CompareType.LIKE));
		conditionList.add(new CompareItem("note", CompareType.LIKE));
		conditionList = ConditionUtil.setConditionValue(conditionList, params);
		return conditionList;
	}

	@SuppressWarnings("rawtypes")
	public List list(Map params) {
		List<CompareItem> conditionList = this.buildConditions(params);
		String fieldSort = ConditionUtil.getFieldSort(params);
		return beanService.list(sqlDao, Role.class, conditionList, fieldSort);
	}

	@SuppressWarnings("rawtypes")
	public PageList listPage(Map params, PageInfo pageInfo) {
		List<CompareItem> coditionList = this.buildConditions(params);
		String fieldSort = ConditionUtil.getFieldSort(params);
		return beanService.listPage(sqlDao, Role.class, coditionList,
				fieldSort, pageInfo);
	}

	/**
	 * 根据用户id获取对应的角色
	 * 
	 * @param user_id
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public List getRoleByUserId(String user_id) {
		return sqlDao.selectList("system.role.getRoleByUserId", user_id);
	}

	/**
	 * 根据用户id获取对应的角色id
	 * 
	 * @param user_id
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public List getRoleIdByUserId(String user_id) {
		return sqlDao.selectList("system.role.getRoleIdByUserId", user_id);
	}

	/**
	 * 获取角色关联的action
	 * 
	 * @param params
	 * @param pageInfo
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public PageList listPageOfAction(Map params, PageInfo pageInfo) {
		return sqlDao.selectPageList("system.role.listOfAction", params,
				pageInfo);
	}

	/**
	 * 添加角色与action关联
	 * 
	 * @param role_id
	 * @param action_id
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public int createActionRelate(String role_id, String action_id) {
		HashMap params = new HashMap();
		params.put("id", StringUtil.getUUID());
		params.put("role_id", role_id);
		params.put("action_id", action_id);
		return sqlDao.insert("system.role.createActionRelate", params);
	}

	/**
	 * 批量添加角色与action关联
	 * 
	 * @param role_id
	 * @param action_id
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public int createActionRelateBatch(String role_id, String[] action_id) {
		HashMap params = new HashMap();
		int i = 0;
		params.put("role_id", role_id);
		for (String item : action_id) {
			params.put("id", StringUtil.getUUID());
			params.put("action_id", item);
			try {
				i = sqlDao.insert("system.role.createActionRelate", params);
			} catch (Exception e) {
			}
		}
		return i;
	}

	/**
	 * 删除角色与action关联
	 * 
	 * @param role_id
	 * @param action_id
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public int deleteActionRelate(String role_id, String action_id) {
		HashMap params = new HashMap();
		params.put("role_id", role_id);
		params.put("action_id", action_id);
		return sqlDao.delete("system.role.deleteActionRelate", params);
	}

	/**
	 * 删除角色与action关联
	 * 
	 * @param role_id
	 * @param action_id
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public int deleteActionRelateBatch(String role_id, String[] action_id) {
		HashMap params = new HashMap();
		int i = 0;
		params.put("role_id", role_id);
		for (String item : action_id) {
			params.put("id", StringUtil.getUUID());
			params.put("action_id", item);
			try {
				i = sqlDao.delete("system.role.deleteActionRelate", params);
			} catch (Exception e) {
			}
		}
		return i;
	}

}
